package com.hmjk.health.views;

import java.text.DecimalFormat;

import com.hmjk.health.R;




import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
//import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;



public class ImageView_XY extends View{
	

	private static int BLUE = 0xff4bd1fe;
//    private static int GREEN = 0xff91c869;	
//    private static int LIGHT_GREEN = 0xffe4e86f;	
//    private static int YELLOW = 0xffe4ce3c;	
///    private static int DEEP_YELLOW = 0xfff5a93c;	
//    private static int RED = 0xfff94244;
//	private int mtextColor = 0xff444444;
	
//    private int total = 0;    
	private int ondraw_height=0;
	private int ondraw_width=0;
	private int ondraw_left=0;
	private int ondraw_right=0 ;
	private int ondraw_top=0;
	private int ondraw_bottom =0;	
	View root_View;
	private ImageView img = null;
	
    private int X = 0;     
    private int Y = 0; 
    private boolean isfocus =  false;     
    private boolean ishide  =  true;     
    
    private int image_normal_res_id = 0;     
    private int image_focus_res_id = 0;    
    static Bitmap dotBmap;
    static Bitmap selectdotBmap;	
    private Drawable mSrc_normal = null, mSrc_focus = null;    
  
//    private int values[]= {0,0,0,0,0,0};
//    private int colors[] = {GREEN,LIGHT_GREEN,YELLOW,DEEP_YELLOW,RED,BLUE};
//    private float arcs[] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
    public void SetXY(int x, int y, boolean is_focus)
    {
    	X = x;
    	Y = y;
    	isfocus = is_focus;
    	Show();
//    	invalidate();
    }

    public int GetX()
    {
    	return X;
    }
    
    public int GetY()
    {
    	return Y;
    }

    public boolean isHided()
    {
    	return ishide;
    }
    
    public void SetXY(int x, int y)
    {
    	X = x;
    	Y = y;
//    	isfocus = true;
    	Show();
    }    
   
    public void Hide()
    {
    	ishide = true;
    	invalidate();
    } 
    
    public void Show()
    {
    	ishide = false;
    	invalidate();
    } 
    
    public void Set_Focus()
    {
    	isfocus = true;
    	invalidate();
    }    

    public void Set_NoFocus()
    {
    	isfocus = false;
    	invalidate();
    }    
    public void Set_Image_Res_ID(int img_normal_id, int img_focus_id)
    {
    	image_normal_res_id = img_normal_id;
    	image_focus_res_id  = img_focus_id;
    }    
    
	private Context mContext;
	private int sysWidth,sysHeight;
	
//	private void init_res()
//	{
//	  ViewGroup vg = new LinearLayout(this); //只要得到一个ViewGroup对象就可以了，一般来说是布局view		setContentView(vg);
//	  setContentView(vg);	
//	}	
    private void initView(Context context) {
        // TODO Auto-generated method stub
		 root_View = (LinearLayout)LayoutInflater.from(mContext).inflate(R.layout.imageview_xy, null);
//		 setContentView(R.layout.imageview_xy);
         dotBmap =       BitmapFactory.decodeResource(getResources(),image_normal_res_id);
         selectdotBmap = BitmapFactory.decodeResource(getResources(),image_focus_res_id);

    }
    
   
 
	void ReadCustomAttr(Context context, AttributeSet attrs)
	{
		
//      	Log.d(TAG,"ReadCustomAttr(Context context, AttributeSet attrs)");	 	        
		
        TypedArray a = context.obtainStyledAttributes(attrs, 
        		R.styleable.imageviewxy); 
 //       mSrc_normal = a.getDrawable(R.styleable.imagexy_normal_src); 
 //       mSrc_focus = a.getDrawable(R.styleable.imagexy_focus_src); 
		
 		
	} 
    
    
    public void Set_image(int x,int y,int normal_resid,int focus_resid)
    {
 ///   	if(img!=null)remove_image();
    	Set_Image_Res_ID(normal_resid, focus_resid);
    	SetXY(x,y);


    }
    

    
    
	private void initSysPara()
	{
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(dm);
		sysWidth = dm.widthPixels;
		sysHeight = dm.heightPixels;
	}
	
	public ImageView_XY(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		initSysPara();
		Set_Image_Res_ID(R.drawable.health_today_dot,R.drawable.health_dot_seleced);
		initView(mContext);		

	}
	
    public ImageView_XY(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;
		
		initSysPara();
		Set_Image_Res_ID(R.drawable.health_today_dot,R.drawable.health_dot_seleced);
	    ReadCustomAttr(context,attrs);  
		initView(mContext);		
	}
	
	public ImageView_XY(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		mContext = context;
		
		initSysPara();
		Set_Image_Res_ID(R.drawable.health_today_dot,R.drawable.health_dot_seleced);
	    ReadCustomAttr(context,attrs);  
		initView(mContext);		

	}    

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);	
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		int hSpec = heightMeasureSpec;
		int wSpec = widthMeasureSpec;
		

		
		if(widthMode == View.MeasureSpec.AT_MOST)
		{
			widthSize = (int) sysWidth/2-10;
			wSpec = View.MeasureSpec.makeMeasureSpec(widthSize, View.MeasureSpec.AT_MOST);
			hSpec = wSpec;
			super.onMeasure(wSpec, hSpec);	
		}		
		else
		super.onMeasure(wSpec, hSpec);
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
	}
	
	@SuppressLint("NewApi")
	@Override
	protected void onDraw(Canvas canvas){

		Paint mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setColor(BLUE);
		//mPaint.setStrokeJoin(Paint.Join.ROUND);   
		//mPaint.setStrokeCap(Paint.Cap.ROUND);   
//		mPaint.setAntiAlias(true);  		
		
		 ondraw_height =getHeight();
		 ondraw_width = getWidth();
		 ondraw_left = getLeft();
		 ondraw_right = getRight();  
		 ondraw_top = getTop();
		 ondraw_bottom = getBottom();  
		
 		super.onDraw(canvas);
 		if(ishide)
 		{
 			
 		}
 		else
 		{	
 		if(isfocus)
 		{
            if(selectdotBmap.getByteCount()>0)
            {
            	canvas.drawBitmap(selectdotBmap, X-selectdotBmap.getWidth()/2, Y-selectdotBmap.getHeight()/2, mPaint);

            }		
 		}
 		else
 		{
             if(dotBmap.getByteCount()>0)
            {
            	canvas.drawBitmap(dotBmap, X-dotBmap.getWidth()/2, Y-dotBmap.getHeight()/2, mPaint);
            }
            	
 		}	
 		}
	}	

}